package com.cal.utms.model.payment;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.cal.utms.dao.payment.PaymentDaoImpl;
import com.cal.utms.dao.payment.PlacementSheetDaoImpl;
import com.cal.utms.dao.payment.ReceiptDaoImpl;
import com.cal.utms.dto.payment.AllCustomer;
import com.cal.utms.dto.payment.TallyReportDto;
import com.cal.utms.entity.Payment;
import com.cal.utms.entity.PlacementSheet;
import com.cal.utms.entity.PlacementSheetRecord;
import com.cal.utms.entity.Receipt;

public class TallyReportModel {

	public void getReportDetails(String tdate, int fundId, TallyReportDto reportDto) {

		PlacementSheetDaoImpl daoImpl = new PlacementSheetDaoImpl();
		ReceiptDaoImpl receiptDaoImpl = new ReceiptDaoImpl();
		PaymentDaoImpl paymentDaoImpl = new PaymentDaoImpl();
		CommenPayementModel customersAction = new CommenPayementModel();
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
		try {
			Date date = format.parse(tdate);

			PlacementSheet placementSheet = daoImpl.getPlacementSheetByFundDate(fundId, date);
			if (placementSheet != null) {
				List<PlacementSheetRecord> list = placementSheet.getPlacementSheetRecords();
				if (list.size() > 0) {
					for (PlacementSheetRecord placementSheetRecord : list) {
						if (placementSheetRecord.getMoneyInOut().equals("IN")) {
							Receipt receipt = receiptDaoImpl.getReceiptByCustomer(placementSheetRecord.getCustomerId(), placementSheet.getFund().getId(), date,
									placementSheetRecord.getCustomerType());
							TallyReportDto tallyReportDto = new TallyReportDto();
							tallyReportDto.setFundid(placementSheet.getFund().getId());
							tallyReportDto.setCustomerId(placementSheetRecord.getCustomerId());
							tallyReportDto.setCustomerType(placementSheetRecord.getCustomerType());
							tallyReportDto.setCustomerName(placementSheetRecord.getCustomer());
							tallyReportDto.setSamount(placementSheetRecord.getAmount());
							tallyReportDto.setSnameOfBank(placementSheetRecord.getNameofbank());
							if (receipt != null) {
								tallyReportDto.setPamount(receipt.getAmount());
								if ((receipt.getAmount().compareTo(placementSheetRecord.getAmount()) == 0) && (receipt.getBankId() == placementSheetRecord.getBankAccount())) {
									tallyReportDto.setStatus("<html><font color='green'>Receipt Created</font></html>");
									tallyReportDto.setPnameOfBank(receipt.getBankAccount().concat(" - " + receipt.getBankName()));
								} else {
									tallyReportDto.setStatus("<html><font color='#F5A30C'>Receipt Not Match</font></html>");
									tallyReportDto.setPnameOfBank(receipt.getBankAccount().concat(" - " + receipt.getBankName()));
								}
							} else {
								tallyReportDto.setPamount(BigDecimal.ZERO);
								tallyReportDto.setPnameOfBank("-");
								tallyReportDto.setStatus("<html><font color='red'>Receipt Not Created</font></html>");
							}
							reportDto.getReportDtosfrom().add(tallyReportDto);
						} else if (placementSheetRecord.getMoneyInOut().equals("OUT")) {
							Payment payment = paymentDaoImpl.getPaymentByCustomer(placementSheetRecord.getCustomerId(), placementSheet.getFund().getId(), date,
									placementSheetRecord.getCustomerType());
							TallyReportDto tallyReportDto = new TallyReportDto();
							tallyReportDto.setFundid(placementSheet.getFund().getId());
							tallyReportDto.setCustomerId(placementSheetRecord.getCustomerId());
							tallyReportDto.setCustomerType(placementSheetRecord.getCustomerType());
							tallyReportDto.setCustomerName(placementSheetRecord.getCustomer());
							tallyReportDto.setSamount(placementSheetRecord.getAmount());
							tallyReportDto.setSnameOfBank(placementSheetRecord.getNameofbank());
							if (payment != null) {
								tallyReportDto.setPamount(payment.getAmount());
								if ((payment.getAmount().compareTo(placementSheetRecord.getAmount()) == 0)
										&& (Integer.parseInt(payment.getBankAccount()) == placementSheetRecord.getBankAccount())) {
									tallyReportDto.setStatus("<html><font color='green'>Payment Created</font></html>");
									tallyReportDto.setPnameOfBank(payment.getBankAccount().concat(" - " + payment.getBankName()));
								} else {
									tallyReportDto.setStatus("<html><font color='#F5A30C'>Payment Not Match</font></html>");
									tallyReportDto.setPnameOfBank(payment.getBankAccount().concat(" - " + payment.getBankName()));
								}
							} else {
								tallyReportDto.setPamount(BigDecimal.ZERO);
								tallyReportDto.setPnameOfBank("-");
								tallyReportDto.setStatus("<html><font color='red'>Payment Not Created</font></html>");
							}
							reportDto.getReportDtosto().add(tallyReportDto);
						}

					}

					List<Payment> payments = paymentDaoImpl.getPaymentRtgs(fundId, date);
					if (payments.size() > 0) {
						for (Payment extpayment : payments) {
							TallyReportDto tallyReportDto = new TallyReportDto();
							tallyReportDto.setFundid(placementSheet.getFund().getId());
							tallyReportDto.setCustomerId(extpayment.getCustomerId());
							tallyReportDto.setCustomerType(extpayment.getCustomerType());
							AllCustomer allCustomer = customersAction.getCustomer(extpayment.getCustomerId(), extpayment.getCustomerType());
							tallyReportDto.setCustomerName(allCustomer.getCustomerName());

							tallyReportDto.setSamount(BigDecimal.ZERO);
							tallyReportDto.setSnameOfBank("-");

							tallyReportDto.setPamount(extpayment.getAmount());

							if (!checkInPlacementSheet(list, extpayment, null)) {
								tallyReportDto.setStatus("<html><font color='red'>Not In Placement Sheet</font></html>");
								tallyReportDto.setPnameOfBank(extpayment.getBankAccount().concat(" - " + extpayment.getBankName()));
								reportDto.getReportDtosto().add(tallyReportDto);
							}
						}
					}

					List<Receipt> receipts = receiptDaoImpl.getRtgsReceipt(fundId, date);
					if (receipts.size() > 0) {
						for (Receipt extreceipt : receipts) {
							TallyReportDto tallyReportDto = new TallyReportDto();
							tallyReportDto.setFundid(placementSheet.getFund().getId());
							tallyReportDto.setCustomerId(extreceipt.getCustomerId());
							tallyReportDto.setCustomerType(extreceipt.getCustomerType());
							AllCustomer allCustomer = customersAction.getCustomer(extreceipt.getCustomerId(), extreceipt.getCustomerType());
							tallyReportDto.setCustomerName(allCustomer.getCustomerName());

							tallyReportDto.setSamount(BigDecimal.ZERO);
							tallyReportDto.setSnameOfBank("-");

							tallyReportDto.setPamount(extreceipt.getAmount());

							if (!checkInPlacementSheet(list, null, extreceipt)) {
								tallyReportDto.setStatus("<html><font color='red'>Not In Placement Sheet</font></html>");
								tallyReportDto.setPnameOfBank(extreceipt.getBankAccount().concat(" - " + extreceipt.getBankName()));
								reportDto.getReportDtosfrom().add(tallyReportDto);
							}
						}
					}

				} else {
					System.out.println("No records");
				}
			} else {
				System.out.println("Null Placement");
			}

		} catch (ParseException e) {
			e.printStackTrace();
		}
	}

	private boolean checkInPlacementSheet(List<PlacementSheetRecord> list, Payment extpayment, Receipt receipt) {
		if (receipt == null) {
			for (PlacementSheetRecord placementSheetRecord : list) {
				if ((extpayment.getAmount().compareTo(placementSheetRecord.getAmount()) == 0)
						&& (Integer.parseInt(extpayment.getBankAccount()) == placementSheetRecord.getBankAccount())) {
					return true;
				}
			}
		}
		if (extpayment == null) {
			for (PlacementSheetRecord placementSheetRecord : list) {
				if ((receipt.getAmount().compareTo(placementSheetRecord.getAmount()) == 0) && (receipt.getBankId() == placementSheetRecord.getBankAccount())) {
					return true;
				}
			}
		}
		return false;
	}
}
